/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.jabber;

import org.json.JSONObject;
import org.webos.java.base.Dialog;
import org.webos.java.base.DialogType;
import org.webos.java.base.NetworkManager;
import org.webos.java.base.RemoteMessage;
import org.webos.java.root.Log;

/**
 *
 * @author Kostya
 */

@DialogType(jsClass="jabber.SettingsDialog")
public class SettingsDialog extends Dialog{

    private String settingName = null;
    private JabberApp app = null;

    public SettingsDialog(JabberApp app, String aSettingName) {
        super(app);
        this.app = app;
        this.settingName = aSettingName;
        JSONObject obj = new JSONObject();
        try {
            obj = new JSONObject(app.getOption(settingName, "{}"));
            obj.put("resource", obj.optString("resource", "Webos"));
            obj.put("autoaway", obj.optBoolean("autoaway", true));
            obj.put("hideoffline", obj.optBoolean("hideoffline", true));
            obj.put("shownotify", obj.optBoolean("shownotify", true));
            obj.put("history_items", obj.optInt("history_items", 5));
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
//        Log.debug(this, "Settings obj = "+obj);
        setData(obj);
        app.getSession().addDialog(this);
    }

    @RemoteMessage
    public JSONObject okClick(JSONObject aData){
        try {
            if(aData.optString("username", "").equals("")){
                return NetworkManager.getErrorObject(801, "Username can't be empty");
            }
            if(aData.optString("resource", "").equals("")){
                return NetworkManager.getErrorObject(804, "Resource can't be empty");
            }

            if(aData.optBoolean("hostport")){
                if(aData.optString("host", "").equals("")){
                    return NetworkManager.getErrorObject(803, "Host can't be empty");
                }
                if(aData.optInt("port", 0)<=0){
                    return NetworkManager.getErrorObject(802, "Port can't be empty");
                }
            }
            JSONObject obj = new JSONObject(app.getOption(settingName, "{}"));
            obj.put("name", aData.opt("name"));
            obj.put("username", aData.opt("username"));
            obj.put("resource", aData.opt("resource"));
            obj.put("autoaway", aData.optBoolean("autoaway"));
            obj.put("hideoffline", aData.optBoolean("hideoffline"));
            obj.put("shownotify", aData.optBoolean("shownotify"));
            obj.put("history", aData.optBoolean("history"));
            obj.put("history_items", aData.optInt("history_items", 0));

            obj.put("hostport", aData.optBoolean("hostport", false));
            obj.put("host", aData.optString("host", ""));
            obj.put("port", aData.optInt("port", 0));
            app.setOption(settingName, obj.toString());
//            Log.debug(this, "Saving object: "+obj);
            if(!aData.optString("password", "").equals("")){
                app.setSecret(aData.getString("username"), aData.optString("password", ""));
            }
            app.getSession().removeDialog(this);
            app.afterSettings(false);
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return null;
    }

    @RemoteMessage
    public void cancelClick(JSONObject aData){
        app.getSession().removeDialog(this);
        app.afterSettings(true);
    }
}
